#version 460
#extension GL_GOOGLE_include_directive : enable

#include "../../../../core/shader_taster_transform.h"
#include "../../../../core/shader_taster_line.h"




layout(location = 0) in vec3 inPos;
layout(location = 1) in uint inCol;


layout(location=0) out vec4 vColor;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};
layout(location=2) out float DepthScale;
layout(location=3) out float Transparency;

//vec3 vs[3] = {vec3(-100,-500,0), vec3(-100,500,0), vec3(500,00,0)};

void main(void) {
	vec4 p = vec4(inPos, 1.0);
	//uint InstanceOffset = gl_BaseInstance;
	uint InstanceOffset = gl_InstanceIndex + drawProp.MatrixOffset;
	//uint InstanceOffset = gl_InstanceIndex;

	gl_PointSize = 3.0;
	S_MeshTranformProp prop = m_LinesProp[drawProp.MeshPropOffset + gl_DrawID];
	mat4 modelMat = mat_Model[InstanceOffset];
	//vec4 pos = mat_ProjecView[0] * vec4(0,0,0,1);
	
	if(prop.m_Color > 0){
		vColor = f_glsl_buildColor(prop.m_Color);
	} else {
		vColor = f_glsl_buildColor(inCol) * prop.m_Intensity;
	}
	DepthScale = prop.m_DepthScale;
	//DepthScale = 1;
	//float d = length(p.xyz - pos.xyz) / 500.0;
	
	p.xyz += vec3(prop.m_XMove, prop.m_YMove, prop.m_ZMove);
	p.xyz *= prop.m_Scale;

	switch(prop.m_TransformMode){
		case DEF_MESH_LINE_TRANFORMTYPEMODEL_NBILLBOARD: {
			p *= mat_ProjecView[1];
			gl_Position = mat_ProjecView[0] * vec4(mat_Model[InstanceOffset][3].xyz + p.xyz, 1);
			break;
		}

		case DEF_MESH_LINE_TRANFORMTYPEMODEL_VERTICAL: {
			p.xyz = vec3(0, -length(mat_Model[InstanceOffset] * p), 0);
			gl_Position = mat_ProjecView[0] * p;
			break;
		}
		
		case DEF_MESH_LINE_TRANFORMTYPEMODEL_GAZE:{
			mat3 rot = mat3(mat_ProjecView[1]);
			//mat3 rot = mat3(inverse(mat_ProjecView[0]));
			vec3 H = (vec3(1, 0, 0) * rot);
			vec3 V = (vec3(0, 0, 1) * rot);
			//V *= rot;

			vec3 dirLine = normalize(mat3(mat_Model[InstanceOffset]) * vec3(0, 1, 0));
			vec3 right = normalize(mat3(mat_Model[InstanceOffset]) * vec3(1, 0, 0));
			H = normalize(cross(dirLine, V));
			
			
			p.xyz = H*p.x + V*p.z + dirLine*p.y;
			p.xyz += mat_Model[InstanceOffset][3].xyz;
			gl_Position = mat_ProjecView[0] * p;
			break;
		}

		case DEF_MESH_LINE_TRANFORMTYPEMODEL_CenterZoom : {
			p.x *= prop.m_XScale;
			p.y *= prop.m_YScale;
			p.z *= prop.m_ZScale;
			gl_Position = mat_ProjecView[0] * mat_Model[InstanceOffset] * p;
			break;
		}

		case DEF_MESH_LINE_TRANFORMTYPEMODEL_ZAxialZoom : {
			p.x *= prop.m_XScale;
			p.y *= prop.m_YScale;
			p.z *= prop.m_ZScale;
			gl_Position = mat_ProjecView[0] * mat_Model[InstanceOffset] * p;
			break;
		}

		case DEF_MESH_LINE_TRANFORMTYPEMODEL_CAMERAGAZE : {
			p.xyz *= mat3(mat_ProjecView[1]);
			//float scale = abs(length(vec3(1) * mat3(mat_ProjecView[1])) - length(vec3(1) * mat3(mat_ProjecView[0])));
			//p.xyz *= scale;
			gl_Position = mat_ProjecView[0] * mat_Model[InstanceOffset] * p;
			break;
		}
		case DEF_MESH_LINE_TRANFORMTYPEMODEL_PointLink : {
			p.xyz = f_lineMeshTranform(inPos, prop, modelMat, mat3(mat_ProjecView[1]), DEF_MESH_LINE_TRANFORMTYPEMODEL_PointLink);
			gl_Position = mat_ProjecView[0] * p;
			break;
		}
		default:{
			gl_Position = mat_ProjecView[0] * mat_Model[InstanceOffset] * p;
			break;
		}
	}


	switch(prop.m_Activate){
		case 1:
			//gl_Position.z += 0.001;
			vec3 z = normalize(inverse(mat3(mat_ProjecView[1])) * vec3(0.0, 0.0, -1.0));
			gl_Position.z += 0.1 / length(gl_Position.xyz - z);
		break;
	}
	
	Transparency = prop.m_Remote;
}


